package com.jf.utils;

import javax.swing.*;
import java.awt.*;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-08-16
 * Time:0:43
 */
public class Card {
    private Graphics2D g2d;

    private final ImageIcon[] front;
    private final ImageIcon[] back;
    private final ImageIcon empty;
    private ImageIcon icon;

    private final int num;

    private final int count;
    private int col, row;
    private int initX, initY;
    private int finalX, finalY;
    private final int moveSpeed = 20;
    private int x, y;

    private final int topGap = 40;
    private final int leftGap = 220;
    private final int rowGap = 125;
    private final int colGap = 150;

    private int flipCount;
    private boolean isBack;

    public Card() {
        this(0, 0);
    }

    public Card(int num, int count) {
        this.num = num;
        this.count = count;

        front = new ImageIcon[7];
        back = new ImageIcon[7];
        empty = new ImageIcon();

        initPosition();
        initImage();

    }

    private void initImage() {
        String path = "/img/trick/card/";
        String frontPath, backPath;
        for (int i = 0; i < 7; i++) {
            frontPath = path + "front" + i + ".png";
            backPath = path + "back" + i + ".png";
            front[i] = new ImageIcon(Objects.requireNonNull(this.getClass().getResource(frontPath)));
            back[i] = new ImageIcon(Objects.requireNonNull(this.getClass().getResource(backPath)));
        }
        icon = back[0];

        isBack = true;
        flipCount = 0;
    }

    private void initPosition() {
        row = count / 5;
        col = count % 5;
        finalX = leftGap + col * colGap;
        finalY = topGap + row * rowGap;
        initX = leftGap + 2 * colGap;
        initY = topGap + 2 * rowGap;
        x = initX;
        y = initY;
    }

    public void draw() {
        g2d.drawImage(icon.getImage(), x, y, null);
        if (icon == front[0]) {
            if (num < 10) {
                g2d.drawString(num + "", finalX + 29, finalY + 75);
            } else {
                g2d.drawString(num + "", finalX + 17, finalY + 75);
            }
        }
    }

    public boolean moveToFinal() {
        if (x == finalX && y == finalY) return true;
        if (x - finalX > 0) {
            x -= moveSpeed;
            if (x < finalX) {
                x = finalX;
            }
        } else {
            x += moveSpeed;
            if (x > finalX) {
                x = finalX;
            }
        }
        if (y - finalY > 0) {
            y -= moveSpeed;
            if (y < finalY) {
                y = finalY;
            }
        } else {
            y += moveSpeed;
            if (y > finalY) {
                y = finalY;
            }
        }
        return false;
    }

    public boolean moveToInit() {
        if (x == initX && y == initY) {
            return true;
        }
        if (x - initX > 0) {
            x -= moveSpeed;
            if (x < initX) {
                x = initX;
            }
        } else {
            x += moveSpeed;
            if (x > initX) {
                x = initX;
            }
        }
        if (y - initY > 0) {
            y -= moveSpeed;
            if (y < initY) {
                y = initY;
            }
        } else {
            y += moveSpeed;
            if (y > initY) {
                y = initY;
            }
        }
        return false;
    }

    public boolean flipToFront() {
        if (icon == front[0]) {
            return true;
        }
        if (isBack) {
            flipCount++;
            if (flipCount < 6) {
                icon = back[flipCount];
            } else {
                icon = empty;
                isBack = false;
            }
        } else {
            flipCount--;
            icon = front[flipCount];
            if (flipCount > 0) {
            } else {
                return true;
            }
        }
        return false;
    }

    public boolean flipToBack() {
        if (icon == back[0]) {
            return true;
        }
        if (!isBack) {
            flipCount++;
            if (flipCount < 6) {
                icon = front[flipCount];
            } else {
                icon = empty;
                isBack = true;
            }
        } else {
            flipCount--;
            if (flipCount > 0) {
                icon = back[flipCount];
            } else {
                return true;
            }
        }
        return false;
    }

    public void setG2d(Graphics g) {
        g2d = (Graphics2D) g;
        g2d.setFont(new Font("微软雅黑", Font.BOLD, 42));
        g2d.setColor(new Color(230, 230, 230));
    }

    public void setCenter() {
        x = 595 + count / 2;
        y = 310 + count;
    }
}
